﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
    public partial class HelpWindow : Form
    {
        EditorState EditorState = null;
        internal String HelpDirectory
        {
            get
            {
                return System.IO.Path.Combine(
                    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
                    "help");
                    
            }
        }

        private void EnumerateHelpDirectory(String Directory, TreeNodeCollection ParentNode)
        {
            var Files = System.IO.Directory.GetFileSystemEntries(Directory);
            foreach (var Path in Files)
            {
                var Attributes = System.IO.File.GetAttributes(Path);
                if (( Attributes & System.IO.FileAttributes.Directory ) != 0)
                {
                    TreeNode NewNode = new TreeNode(System.IO.Path.GetFileName(Path));
                    NewNode.Tag = "file://" + System.IO.Path.Combine(Path, "index.html");
                    EnumerateHelpDirectory(Path, NewNode.Nodes);
                    ParentNode.Add(NewNode);
                }
                else if (System.IO.Path.GetExtension(Path) == ".html")
                {
                    String FileName = System.IO.Path.GetFileNameWithoutExtension(Path);
                    if (FileName != "index")
                    {
                        TreeNode NewNode = new TreeNode(FileName);
                        NewNode.Tag = "file://" + Path;
                        ParentNode.Add(NewNode);
                    }
                }
            }
        }

        public void SetEditorState(EditorState ES)
        {
            EditorState = ES;
            TreeNode root = new TreeNode("Help Topics");
            root.Tag = "file://" + System.IO.Path.Combine(HelpDirectory, "index.html");
            TopicTree.Nodes.Add(root);

            EnumerateHelpDirectory(HelpDirectory, root.Nodes);

            var Codeplex = new TreeNode("Jemgine on Codeplex");
            Codeplex.Tag = "http://jemgine.codeplex.com/";
            TopicTree.Nodes.Add(Codeplex);

            var Blog = new TreeNode("Jemgine dev blog");
            Blog.Tag = "http://m.jemgine.omnisu.com/";
            TopicTree.Nodes.Add(Blog);
        }

        public HelpWindow()
        {
            InitializeComponent();
        }

        public void SelectTopic(String Topic)
        {
            ContentPanel.Navigate("file://" + System.IO.Path.Combine(HelpDirectory, Topic));
            Invalidate();
        }

        private void HelpWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            EditorState.HelpWindow = null;
        }

        private void TopicTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            String Topic = e.Node.Tag as String;
            ContentPanel.Navigate(Topic);
            ContentPanel.Invalidate();
            Invalidate();
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            ContentPanel.GoBack();
        }

        private void ForwardButton_Click(object sender, EventArgs e)
        {
            ContentPanel.GoForward();
        }

        private void HomeButton_Click(object sender, EventArgs e)
        {
            SelectTopic("index.html");
        }
   }
}
